package com.miao.utils;


import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
* Description:  MyBatis 的工具类  获取sqlSession对象
* Author: Miao@ShaoQing
* Data: 17:36 2021/6/28
*/
public class MyBatisUtils {

    private static SqlSessionFactory sqlSessionFactory;

    static {
        String resource = "mybatis-config.xml";
        try {
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * Description:   工具类的静态方法 返回sqlSession对象
     * Author: Miao@ShaoQing
     * Data: 17:36 2021/6/28
     */
    public static SqlSession getSqlSession(){
        return sqlSessionFactory.openSession();
    }



}
